import axios from 'axios'
var url = '/api'
// axios 配置
axios.defaults.timeout = 5000
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.baseURL = url
// 这里的url指接口地址

export function Post(url, params) {
  return new Promise((resolve, reject) => {
    axios.post(url, params)
      .then(response => {
        if (response.data.status === 200) {
          resolve(response.data)
        } else {
        }
      }, err => {
        reject(err)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

export function Get(url, params) {
  return new Promise((resolve, reject) => {
    axios.get(url, params)
      .then(response => {
        if (response.status === 200) {
          resolve(response.data)
        } else {
        }
      }, err => {
        reject(err)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

export default {
  connect(params) {
    // 链接
    return Get(`/connect?ProductKey=${params.ProductKey}&DeviceName=${params.DeviceName}&DeviceSecret=${params.DeviceSecret}&Topic=${params.Topic}`)
  },
  message() {
    // 接收消息
    return Get('/message')
  },
  subscribe(params) {
    // 订阅
    return Get(`/subscribe?Topic=${params.Topic}`)
  },
  publish(params) {
    // 发布
    return Get(`/publish?Topic=${params.Topic}&publishMsg=${params.publishMsg}`)
  },
  end() {
    // 发布
    return Get(`/end`)
  },
}
